package com.example.controller;

import com.example.dto.OrderDTO;
import com.example.entity.Order;
import com.example.order.api.OrderApi;
import com.example.service.OrderService;
import com.example.vo.OrderIVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@Api(value = "订单下单接口")
public class OrderController implements OrderApi {

    @Autowired
    OrderService orderService;

    @ApiOperation("用户下单接口")
    @PostMapping(value = "/createOrder")
    public String createOrder(@RequestBody @Valid OrderIVO orderIVO){
        OrderDTO obj=new OrderDTO();
        obj.setUserId(1);
        obj.setProductId(1);
        obj.setPaymentMoney(new BigDecimal(100));
        obj.setProductCount(1);
        //扣除2个积分
        obj.setDecreaseScore(2);
        //抵扣优惠卷
        obj.setCouponReceiveId(orderIVO.getCouponId());
        obj.setOrderNo(UUID.randomUUID().toString().replaceAll("-",""));
        orderService.createOrder(obj);
        return "ok";
    }

    @Override
    public String createOrder(Integer c) {
        return null;
    }

    @Override
    public List<com.example.order.dto.OrderDTO> unpayOrderList() {
        List<Order> orders= this.orderService.unpayOrderList();
        List<com.example.order.dto.OrderDTO> orderList= new ArrayList<com.example.order.dto.OrderDTO>();
        for(Order order:orders){
            com.example.order.dto.OrderDTO obj=new com.example.order.dto.OrderDTO();
            BeanUtils.copyProperties(order,obj);
            orderList.add(obj);
        }
        return  orderList;
    }

    @Override
    public List<com.example.order.dto.OrderDTO> orderList() {
        List<Order> orders= this.orderService.findAll();
        List<com.example.order.dto.OrderDTO> orderList= new ArrayList<com.example.order.dto.OrderDTO>();
        for(Order order:orders){
            com.example.order.dto.OrderDTO obj=new com.example.order.dto.OrderDTO();
            BeanUtils.copyProperties(order,obj);
            orderList.add(obj);
        }
        return  orderList;
    }

    @Override
    public com.example.order.dto.OrderDTO findOrderNo(String orderNo) {
        Order order=this.orderService.findOrderNo(orderNo);
        com.example.order.dto.OrderDTO obj=new com.example.order.dto.OrderDTO();
        BeanUtils.copyProperties(order,obj);
        return obj;
    }
}
